package com.fw.ui.view.common;

import static org.springframework.beans.factory.config.BeanDefinition.SCOPE_PROTOTYPE;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.fw.service.bean.SearchCriteriaBean;
import com.fw.ui.bao.MessageBAO;
import com.fw.ui.bao.PropertyBAO;
import com.fw.ui.view.AbstractPanel;
import com.fw.ui.view.common.event.SortingTablePanelSortingSelectBlurListener;
import com.fw.ui.view.common.event.SortingTablePanelSortingSelectValueChangeListener;
import com.fw.ui.view.event.AbstractSortTableButtonClickListener;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.OptionGroup;
import com.vaadin.ui.VerticalLayout;

@Component
@Scope(SCOPE_PROTOTYPE)
public class SortingTablePanel extends AbstractPanel {

    private static final long serialVersionUID = 1L;

    @Autowired
    private MessageBAO messageBAO;
    @Autowired
    private PropertyBAO propertyBAO;

    private ComboBox cbbSelectSort1 = new ComboBox();
    private ComboBox cbbSelectSort2 = new ComboBox();
    private ComboBox cbbSelectSort3 = new ComboBox();

    private OptionGroup opgSortingType1 = new OptionGroup();
    private OptionGroup opgSortingType2 = new OptionGroup();
    private OptionGroup opgSortingType3 = new OptionGroup();

    private Map<String, String> mapItems = new LinkedHashMap<String, String>();

    private Button btnSorting = new Button();

    @Autowired
    private SortingTablePanelSortingSelectBlurListener sortingTablePanelSortingSelectBlurListener;
    @Autowired
    private SortingTablePanelSortingSelectValueChangeListener sortingTablePanelSortingSelectValueChangeListener;

    @Override
    public void init() {
	VerticalLayout layout = new VerticalLayout();
	layout.setSizeFull();

	HorizontalLayout hLayout = new HorizontalLayout();
	hLayout.setSizeUndefined();
	hLayout.setSpacing(true);

	HorizontalLayout hSubLayout1 = new HorizontalLayout();
	HorizontalLayout hSubLayout2 = new HorizontalLayout();
	HorizontalLayout hSubLayout3 = new HorizontalLayout();
	HorizontalLayout hSubLayout4 = new HorizontalLayout();

	hSubLayout1.setSpacing(true);
	hSubLayout2.setSpacing(true);
	hSubLayout3.setSpacing(true);
	hSubLayout4.setSpacing(true);

	btnSorting.setDisableOnClick(true);
	btnSorting.addStyleName("button-blue");
	btnSorting.setWidth("80px");

	cbbSelectSort1.setTextInputAllowed(false);
	cbbSelectSort2.setTextInputAllowed(false);
	cbbSelectSort3.setTextInputAllowed(false);

	cbbSelectSort1.setImmediate(true);
	cbbSelectSort2.setImmediate(true);
	cbbSelectSort3.setImmediate(true);

	hSubLayout1.addComponent(cbbSelectSort1);
	hSubLayout1.addComponent(opgSortingType1);
	hSubLayout1.setComponentAlignment(cbbSelectSort1, Alignment.MIDDLE_CENTER);
	hSubLayout1.setComponentAlignment(opgSortingType1, Alignment.MIDDLE_CENTER);

	hSubLayout2.addComponent(cbbSelectSort2);
	hSubLayout2.addComponent(opgSortingType2);
	hSubLayout2.setComponentAlignment(cbbSelectSort2, Alignment.MIDDLE_CENTER);
	hSubLayout2.setComponentAlignment(opgSortingType2, Alignment.MIDDLE_CENTER);

	hSubLayout3.addComponent(cbbSelectSort3);
	hSubLayout3.addComponent(opgSortingType3);
	hSubLayout3.setComponentAlignment(cbbSelectSort3, Alignment.MIDDLE_CENTER);
	hSubLayout3.setComponentAlignment(opgSortingType3, Alignment.MIDDLE_CENTER);

	hSubLayout4.addComponent(btnSorting);
	hSubLayout4.setComponentAlignment(btnSorting, Alignment.MIDDLE_CENTER);

	Label lblSep1 = new Label("");
	Label lblSep2 = new Label("");
	Label lblSep3 = new Label("");

	hLayout.addComponent(hSubLayout1);
	hLayout.addComponent(lblSep1);
	hLayout.addComponent(hSubLayout2);
	hLayout.addComponent(lblSep2);
	hLayout.addComponent(hSubLayout3);
	hLayout.addComponent(lblSep3);
	hLayout.addComponent(hSubLayout4);

	hLayout.setComponentAlignment(hSubLayout1, Alignment.MIDDLE_CENTER);
	hLayout.setComponentAlignment(hSubLayout2, Alignment.MIDDLE_CENTER);
	hLayout.setComponentAlignment(hSubLayout3, Alignment.MIDDLE_CENTER);
	hLayout.setComponentAlignment(hSubLayout4, Alignment.MIDDLE_CENTER);

	hLayout.setComponentAlignment(lblSep1, Alignment.MIDDLE_CENTER);
	hLayout.setComponentAlignment(lblSep2, Alignment.MIDDLE_CENTER);
	hLayout.setComponentAlignment(lblSep3, Alignment.MIDDLE_CENTER);

	layout.addComponent(hLayout);
	layout.setComponentAlignment(hLayout, Alignment.MIDDLE_CENTER);

	this.setContent(layout);
    }

    @Override
    public void initListener() {
	sortingTablePanelSortingSelectBlurListener.setBasedComponent(this);
	sortingTablePanelSortingSelectValueChangeListener.setBasedComponent(this);

	this.cbbSelectSort1.addValueChangeListener(sortingTablePanelSortingSelectValueChangeListener);
	this.cbbSelectSort1.addBlurListener(sortingTablePanelSortingSelectBlurListener);

	this.cbbSelectSort2.addValueChangeListener(sortingTablePanelSortingSelectValueChangeListener);
	this.cbbSelectSort2.addBlurListener(sortingTablePanelSortingSelectBlurListener);

	this.cbbSelectSort3.addValueChangeListener(sortingTablePanelSortingSelectValueChangeListener);
	this.cbbSelectSort3.addBlurListener(sortingTablePanelSortingSelectBlurListener);
    }

    @Override
    public void initValidator() {
	// TODO Auto-generated method stub

    }

    @Override
    public void initData() {
	opgSortingType1.addItem(SearchCriteriaBean.DIRECTION_DESC);
	opgSortingType1.addItem(SearchCriteriaBean.DIRECTION_ASC);
	opgSortingType1.select(SearchCriteriaBean.DIRECTION_DESC);

	opgSortingType2.addItem(SearchCriteriaBean.DIRECTION_DESC);
	opgSortingType2.addItem(SearchCriteriaBean.DIRECTION_ASC);
	opgSortingType2.select(SearchCriteriaBean.DIRECTION_DESC);

	opgSortingType3.addItem(SearchCriteriaBean.DIRECTION_DESC);
	opgSortingType3.addItem(SearchCriteriaBean.DIRECTION_ASC);
	opgSortingType3.select(SearchCriteriaBean.DIRECTION_DESC);
    }

    @Override
    public void updateStrings() {
	btnSorting.setCaption(this.messageBAO.getMessage("com.fw.ui.view.common.TablePanel.sorting.button.caption"));
	cbbSelectSort1.setCaption(this.messageBAO.getMessage("com.fw.ui.view.common.TablePanel.sorting.select.sort1.caption"));
	cbbSelectSort2.setCaption(this.messageBAO.getMessage("com.fw.ui.view.common.TablePanel.sorting.select.sort2.caption"));
	cbbSelectSort3.setCaption(this.messageBAO.getMessage("com.fw.ui.view.common.TablePanel.sorting.select.sort3.caption"));

	cbbSelectSort1.setInputPrompt(this.messageBAO.getMessage("com.fw.ui.view.common.SortingTablePanel.inputprompt.sorting1"));
	cbbSelectSort2.setInputPrompt(this.messageBAO.getMessage("com.fw.ui.view.common.SortingTablePanel.inputprompt.sorting2"));
	cbbSelectSort3.setInputPrompt(this.messageBAO.getMessage("com.fw.ui.view.common.SortingTablePanel.inputprompt.sorting3"));
    }

    public void addSortingItem(String itemId, String caption) {
	this.mapItems.put(itemId, caption);

	cbbSelectSort1.addItem(itemId);
	cbbSelectSort1.setItemCaption(itemId, caption);

	cbbSelectSort2.addItem(itemId);
	cbbSelectSort2.setItemCaption(itemId, caption);

	cbbSelectSort3.addItem(itemId);
	cbbSelectSort3.setItemCaption(itemId, caption);
    }

    public Map<String, String> getItems() {
	return this.mapItems;
    }

    public void sortingItemChange() {
	if (mapItems != null && !mapItems.isEmpty()) {
	    Object selectObject1 = cbbSelectSort1.getValue();
	    Object selectObject2 = cbbSelectSort2.getValue();
	    Object selectObject3 = cbbSelectSort3.getValue();

	    cbbSelectSort1.removeBlurListener(sortingTablePanelSortingSelectBlurListener);
	    cbbSelectSort1.removeValueChangeListener(sortingTablePanelSortingSelectValueChangeListener);
	    cbbSelectSort2.removeBlurListener(sortingTablePanelSortingSelectBlurListener);
	    cbbSelectSort2.removeValueChangeListener(sortingTablePanelSortingSelectValueChangeListener);
	    cbbSelectSort3.removeBlurListener(sortingTablePanelSortingSelectBlurListener);
	    cbbSelectSort3.removeValueChangeListener(sortingTablePanelSortingSelectValueChangeListener);

	    cbbSelectSort1.removeAllItems();
	    cbbSelectSort2.removeAllItems();
	    cbbSelectSort3.removeAllItems();

	    for (Entry<String, String> entry : mapItems.entrySet()) {
		if (selectObject1 != null && entry.getKey().equals(selectObject1)) {
		    cbbSelectSort1.addItem(entry.getKey());
		    cbbSelectSort1.setItemCaption(entry.getKey(), entry.getValue());
		    cbbSelectSort1.setValue(selectObject1);
		    cbbSelectSort1.select(selectObject1);
		} else if (selectObject2 != null && entry.getKey().equals(selectObject2)) {
		    cbbSelectSort2.addItem(entry.getKey());
		    cbbSelectSort2.setItemCaption(entry.getKey(), entry.getValue());
		    cbbSelectSort2.setValue(selectObject2);
		    cbbSelectSort2.select(selectObject2);
		} else if (selectObject3 != null && entry.getKey().equals(selectObject3)) {
		    cbbSelectSort3.addItem(entry.getKey());
		    cbbSelectSort3.setItemCaption(entry.getKey(), entry.getValue());
		    cbbSelectSort3.setValue(selectObject3);
		    cbbSelectSort3.select(selectObject3);
		} else {
		    cbbSelectSort1.addItem(entry.getKey());
		    cbbSelectSort1.setItemCaption(entry.getKey(), entry.getValue());
		    cbbSelectSort2.addItem(entry.getKey());
		    cbbSelectSort2.setItemCaption(entry.getKey(), entry.getValue());
		    cbbSelectSort3.addItem(entry.getKey());
		    cbbSelectSort3.setItemCaption(entry.getKey(), entry.getValue());
		}
	    }

	    this.cbbSelectSort1.addValueChangeListener(sortingTablePanelSortingSelectValueChangeListener);
	    this.cbbSelectSort1.addBlurListener(sortingTablePanelSortingSelectBlurListener);

	    this.cbbSelectSort2.addValueChangeListener(sortingTablePanelSortingSelectValueChangeListener);
	    this.cbbSelectSort2.addBlurListener(sortingTablePanelSortingSelectBlurListener);

	    this.cbbSelectSort3.addValueChangeListener(sortingTablePanelSortingSelectValueChangeListener);
	    this.cbbSelectSort3.addBlurListener(sortingTablePanelSortingSelectBlurListener);

	}
    }

    public void addSortTableButtonClickListener(AbstractSortTableButtonClickListener sortTableButtonClickListener) {
	this.btnSorting.addClickListener(sortTableButtonClickListener);
    }

    public ComboBox getCbbSelectSort1() {
	return cbbSelectSort1;
    }

    public ComboBox getCbbSelectSort2() {
	return cbbSelectSort2;
    }

    public ComboBox getCbbSelectSort3() {
	return cbbSelectSort3;
    }

    public OptionGroup getOpgSortingType1() {
	return opgSortingType1;
    }

    public OptionGroup getOpgSortingType2() {
	return opgSortingType2;
    }

    public OptionGroup getOpgSortingType3() {
	return opgSortingType3;
    }

}
